﻿using DFGK_WMS.IRepository.IService;
using DFGK_WMS.Model.Enum;
using DFGK_WMS.Model.Models;
using log4net.Util;
using NPOI.SS.Formula.Functions;
using SqlSugar;

namespace DFGK_WMS.Repository.Service
{
    public class RemoveInventoryService : BaseService, IRemoveInventoryService
    {
        public RemoveInventoryService(ISqlSugarClient cliet) : base(cliet)
        {

        }
        /// <summary>
        /// 获取移库可用仓位
        /// </summary>
        /// <param name="locationId">仓位id</param>
        /// <param name="toLogicalPartCode">逻辑分区编码</param>
        /// <param name="isPriorityFilling">true 深货有位  false整列全空</param>
        /// <param name="filterType">1同物料同批次 2同物料不同批次 3不限物料不限批次</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public  dynamic GetRmoveEnableCell(int locationId, string toLogicalPartCode, bool isPriorityFilling = false, int filterType = 1)
        {
            TInventory inventory = _Client.Queryable<TInventory>().First(t => t.Id == locationId);
            if (inventory == null)
            {
                throw new Exception("移库ID错误");
            }
            var cellQuery = _Client.Queryable<TCell>();
            if (inventory.IsMultPallet == true)
            {
                toLogicalPartCode = "MultPallet";//花盘定死
            }
            //仓位
            string? cellcode = inventory.LocationCode;
            //巷道id
            if (cellcode == null) 
            {
                return null;
            }
            int? rouwayid = _Client.Queryable<TCell>().First(x => x.Code == cellcode).RoutewayId;
            if (rouwayid == null)
            {
                return null;
            }
            var tcellobj=_Client.Queryable<TCell>().Where(x=>x.RoutewayId==rouwayid && x.IsMaxDepth==true && x.FullStatus==0 && x.IsLock==false).ToList();
            
            return tcellobj.Select(x=>new { 
                Code=x.Code
            });

        }
    }
}
